Trait isotope_parser::prettyprint::DocAllocator [−]
pub trait DocAllocator<'a, A = ()> where
A: 'a, { type Doc: DocPtr<'a, A>;}Show methods
fn alloc(&'a self, doc: Doc<'a, Self::Doc, A>) -> Self::Doc; fn alloc_column_fn(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> <Self::Doc as DocPtr<'a, A>>::ColumnFn; fn alloc_width_fn(
&'a self,
f: impl Fn(isize) -> Self::Doc + 'a
) -> <Self::Doc as DocPtr<'a, A>>::WidthFn; fn alloc_cow(&'a self, doc: BuildDoc<'a, Self::Doc, A>) -> Self::Doc { ... } fn nil(&'a self) -> DocBuilder<'a, Self, A> { ... } fn fail(&'a self) -> DocBuilder<'a, Self, A> { ... } fn hardline(&'a self) -> DocBuilder<'a, Self, A> { ... } fn space(&'a self) -> DocBuilder<'a, Self, A> { ... } fn line(&'a self) -> DocBuilder<'a, Self, A> { ... } fn line_(&'a self) -> DocBuilder<'a, Self, A> { ... } fn softline(&'a self) -> DocBuilder<'a, Self, A> { ... } fn softline_(&'a self) -> DocBuilder<'a, Self, A> { ... } fn as_string<U>(&'a self, data: U) -> DocBuilder<'a, Self, A>
where
U: Display, { ... } fn text<U>(&'a self, data: U) -> DocBuilder<'a, Self, A>
where
U: Into<Cow<'a, str>>, { ... } fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A>
where
I: IntoIterator,
<I as IntoIterator>::Item: Into<BuildDoc<'a, Self::Doc, A>>, { ... } fn intersperse<I, S>(
&'a self,
docs: I,
separator: S
) -> DocBuilder<'a, Self, A>
where
I: IntoIterator,
S: Into<BuildDoc<'a, Self::Doc, A>> + Clone,
<I as IntoIterator>::Item: Into<BuildDoc<'a, Self::Doc, A>>, { ... } fn column(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> DocBuilder<'a, Self, A> { ... } fn nesting(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> DocBuilder<'a, Self, A> { ... } fn reflow(&'a self, text: &'a str) -> DocBuilder<'a, Self, A>
where
A: Clone,
Self::Doc: Clone, { ... }
Expand description
The DocAllocator
trait abstracts over a type which can allocate (pointers to) Doc
.
Associated Types
Required methods
Provided methods
fn nil(&'a self) -> DocBuilder<'a, Self, A>
fn nil(&'a self) -> DocBuilder<'a, Self, A>
Allocate an empty document.
fn fail(&'a self) -> DocBuilder<'a, Self, A>
fn fail(&'a self) -> DocBuilder<'a, Self, A>
Fails document rendering immediately.
Primarily used to abort rendering inside the left side of Union
fn hardline(&'a self) -> DocBuilder<'a, Self, A>
fn hardline(&'a self) -> DocBuilder<'a, Self, A>
Allocate a single hardline.
fn space(&'a self) -> DocBuilder<'a, Self, A>
fn line(&'a self) -> DocBuilder<'a, Self, A>
fn line(&'a self) -> DocBuilder<'a, Self, A>
A line acts like a \n
but behaves like space
if it is grouped on a single line.
fn line_(&'a self) -> DocBuilder<'a, Self, A>
fn line_(&'a self) -> DocBuilder<'a, Self, A>
Acts like line
but behaves like nil
if grouped on a single line
use pretty::{Doc, RcDoc}; let doc = RcDoc::<()>::group( RcDoc::text("(") .append( RcDoc::line_() .append(Doc::text("test")) .append(Doc::line()) .append(Doc::text("test")) .nest(2), ) .append(Doc::line_()) .append(Doc::text(")")), ); assert_eq!(doc.pretty(5).to_string(), "(\n test\n test\n)"); assert_eq!(doc.pretty(100).to_string(), "(test test)");
fn softline(&'a self) -> DocBuilder<'a, Self, A>
fn softline(&'a self) -> DocBuilder<'a, Self, A>
A softline
acts like space
if the document fits the page, otherwise like line
fn softline_(&'a self) -> DocBuilder<'a, Self, A>
fn softline_(&'a self) -> DocBuilder<'a, Self, A>
A softline_
acts like nil
if the document fits the page, otherwise like line_
fn as_string<U>(&'a self, data: U) -> DocBuilder<'a, Self, A> where
U: Display,
fn as_string<U>(&'a self, data: U) -> DocBuilder<'a, Self, A> where
U: Display,
Allocate a document containing the text t.to_string()
.
The given text must not contain line breaks.
fn text<U>(&'a self, data: U) -> DocBuilder<'a, Self, A> where
U: Into<Cow<'a, str>>,
fn text<U>(&'a self, data: U) -> DocBuilder<'a, Self, A> where
U: Into<Cow<'a, str>>,
Allocate a document containing the given text.
The given text must not contain line breaks.
fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A> where
I: IntoIterator,
<I as IntoIterator>::Item: Into<BuildDoc<'a, Self::Doc, A>>,
fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A> where
I: IntoIterator,
<I as IntoIterator>::Item: Into<BuildDoc<'a, Self::Doc, A>>,
Allocate a document concatenating the given documents.
fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A> where
I: IntoIterator,
S: Into<BuildDoc<'a, Self::Doc, A>> + Clone,
<I as IntoIterator>::Item: Into<BuildDoc<'a, Self::Doc, A>>,
fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A> where
I: IntoIterator,
S: Into<BuildDoc<'a, Self::Doc, A>> + Clone,
<I as IntoIterator>::Item: Into<BuildDoc<'a, Self::Doc, A>>,
Allocate a document that intersperses the given separator S
between the given documents
[A, B, C, ..., Z]
, yielding [A, S, B, S, C, S, ..., S, Z]
.
Compare the intersperse
method from the itertools
crate.
NOTE: The separator type, S
may need to be cloned. Consider using cheaply cloneable ptr
like RefDoc
or RcDoc
fn column(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> DocBuilder<'a, Self, A>
fn column(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> DocBuilder<'a, Self, A>
Allocate a document that acts differently based on the position and page layout
use pretty::DocAllocator; let arena = pretty::Arena::<()>::new(); let doc = arena.text("prefix ") .append(arena.column(|l| { arena.text("| <- column ").append(arena.as_string(l)).into_doc() })); assert_eq!(doc.1.pretty(80).to_string(), "prefix | <- column 7");
fn nesting(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> DocBuilder<'a, Self, A>
fn nesting(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a
) -> DocBuilder<'a, Self, A>
Allocate a document that acts differently based on the current nesting level
use pretty::DocAllocator; let arena = pretty::Arena::<()>::new(); let doc = arena.text("prefix ") .append(arena.nesting(|l| { arena.text("[Nested: ").append(arena.as_string(l)).append("]").into_doc() }).nest(4)); assert_eq!(doc.1.pretty(80).to_string(), "prefix [Nested: 4]");